package com.road.yishi.route;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.road.yishi.conn.ClientSet;
import com.road.yishi.execaction.Command;
import com.road.yishi.mina.LinkedClient;
import com.road.yishi.proto.PBMessage;

/**
 * @Description :
 * @FileName: RouteHandler.java
 * @Author :WeiHui.Zhang
 * @Data : 2016年2月26日 下午3:57:47
 * @Version:V1.00
 */
public abstract class RouteHandler extends IoHandlerAdapter {
	
	
	protected static final Logger LOGGER = LoggerFactory.getLogger(RouteHandler.class);

	protected String handlerName;

	public RouteHandler(String handlerName) {
		this.handlerName = handlerName;
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		PBMessage packet = (PBMessage) message;

		// 记录指定用户的数据包
//		if (UserMgr.isRecord(packet.getPlayerId())) {
//			String record = null;
//			if (UserMgr.isRecordDetail()) {
//				record = packet.detailToStr();
//			} else {
//				record = packet.headerToStr();
//			}
//			Log.fatal(handName + " : " + record);
//		}
		// 记录包相关信息
//		PacketLogMgr.receivePacketLog(packet, "RouteHandler");
		// 数据包按协议号转发
		int code = packet.getCode();
		/*
		 * if (code > 0 && code < 5000) { ClientSet.routeClient(packet); } else
		 * if (code > 5001 && code < 10000) { ClientSet.routeCastle(packet); }
		 * else if (code > 10001 && code < 15000) {
		 * ClientSet.routeBattle(packet); } else if (code > 15001 && code <
		 * 20000) { ClientSet.routeChat(packet); } else if (code >= 20001 &&
		 * code < 25000) { handle(session, packet); } else if (code >= 25001 &&
		 * code < 30000) { ClientSet.routeCross(packet); } else { Log.info(
		 * "Illegal Packet. packet header : " + packet.headerToStr()); }
		 */

		String name = "";
		if (code > 0 && code < 5000) {
			ClientSet.routeClient(packet);
		} else if (code > 5001 && code < 10000) {
			ClientSet.routeCastle(packet);
			name = "逻辑服";
		} else if (code > 10001 && code < 15000) {
		//	ClientSet.routeBattle(packet);
			name = "战斗服";
		} else if (code > 15001 && code < 20000) {
		//	ClientSet.routeChat(packet);
			name = "聊天服";
		} else if (code >= 20001 && code < 25000) {
			handle(session, packet);
			name = "网关服";
		} else if (code >= 25001 && code < 30000) {
		//	ClientSet.routeCross(packet);
			name = "跨服";
		} else {
			LOGGER.info("Illegal Packet. packet header : " + packet.headerToStr());
		}
		System.err.println("client=" + name + "----->" + code + "----->" + Integer.toHexString(code));

	}

	/**
	 * <pre>
	 * 网关上需要解包协议处理
	 * </pre>
	 *
	 * @param session
	 * @param packet
	 */
	protected void handle(IoSession session, PBMessage packet) {
		short code = packet.getCode();
//		Command cmd = CommandSet.getCommand(code);
//		if (cmd != null) {
//			try {
//				CmdHandlerMgr.addCmd(cmd, session, packet);
//			} catch (Exception e) {
//				Log.error("code = " + code + " has exception:", e);
//			}
//		}
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		if (session != null) {
			LinkedClient client = (LinkedClient) session.getAttribute(LinkedClient.KEY_CLIENT);
			if (client != null) {
				client.disConnect();
			}
		}
	}
}
